# -*- coding: utf-8 -*-
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db

from emof import core as EMOF
from emof import dumper
from emof import loader

from model import Model

class LoadPropertyData(webapp.RequestHandler):
    def get(self):
        m_id = self.request.get("m_id")
        a_id = self.request.get("id")
        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        self.response.headers['Content-Type'] = 'application/xml'
        xml = "<r></r>"
        
        a = rep.findProperty(a_id)
        template_values = {'id': a_id,
                           'p': a}
        path = os.path.join(os.getcwd(), 'xml/Property.xml')
        xml = template.render(path, template_values)
                
        self.response.out.write(xml)
                
class SavePropertyData(webapp.RequestHandler):
    def post(self):
        m_id = self.request.get("m_id")
        a_id = self.request.get("id")
        name = self.request.get("name")
        type = self.request.get("type")
        if self.request.get("isOrdered") == '1':
            isOrdered = True
        else:
            isOrdered = False
        if self.request.get("isUnique") == '1':
            isUnique = True
        else:
            isUnique = False
        lower = self.request.get("lower")
        upper = self.request.get("upper")
        if self.request.get("isReadOnly"):
            isReadOnly = True
        else:
            isReadOnly = False
        default = self.request.get("default")
        if self.request.get("isComposite"):
            isComposite = True
        else:
            isComposite = False
        if self.request.get("isDerived"):
            isDerived = True
        else:
            isDerived = False
        if self.request.get("isID"):
            isID = True
        else:
            isID = False
        
        opposite = self.request.get("opposite")

        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        
        a = rep.findProperty(a_id)
        
        if type:
            t = rep.findType(type)
            if t:
                a.type = t
        else:
            a.type = None
        
        a.name = name
        a.isOrdered = isOrdered
        a.isUnique = isUnique
        a.lower = lower
        a.upper = upper
        a.isReadOnly = isReadOnly
        a.default = default
        a.isComposite = isComposite
        a.isDerived = isDerived
        a.isId = isID
        
        if opposite != "":
            att = rep.findProperty(opposite)
            if att:
                a.opposite = att
                att.opposite = a
        else:
            if a.opposite:
                a.opposite.opposite = None
            a.opposite = None
                    
        model.model_XMI = dumper.Dumper().dump(rep)
        model.put()
                
        self.response.out.write("OK")
        
class CreatePropertyData(webapp.RequestHandler):
    def get(self):
        m_id = self.request.get("m_id")
        
        if self.request.get("p_id") != "":
            p_id = self.request.get("p_id")
        else:
            p_id = '-1'
            
        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        fact = EMOF.Factory(rep)       
        a = fact.create_Property()
        a.name = 'New Property'
        a.isOrdered = False
        a.isUnique = True
        a.lower = 1
        a.upper = 1
        a.isReadOnly = False
        a.isComposite = False
        a.isDerived = False
        
        clss = rep.findClass(p_id)

        if clss:
            clss.ownedAttribute.append(a)
            a.class_ = clss
        model.model_XMI = dumper.Dumper().dump(rep)
        model.put()
                    
        self.response.out.write(a.id + '$$$$' + a.name)
        
class LoadAllProperties(webapp.RequestHandler):
    def get(self):
        m_id = self.request.get("m_id")
        id = self.request.get("id")
        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        self.response.headers['Content-Type'] = 'application/xml'
        xml = "<r></r>"
        
        properties = list()
        
        clss = rep.findClass(id)
        if clss:
            properties = clss.ownedAttribute
         
        template_values = {'properties': properties}
        
        path = os.path.join(os.getcwd(), 'xml/getProperty.xml')
        xml = template.render(path, template_values)
        self.response.out.write(xml)